package page

import (
	"net/url"
	"os"
	"path/filepath"

	"gitee.com/haodreams/golib/easyroute/group"
	"github.com/gin-gonic/gin"
)

type MdController struct {
	group.Group
}

// DefaultGateway 默认网关入口
func (m *MdController) DefaultGateway(c *gin.Context) {
	// 检查路径是否有权限
	if !m.CheckPath(c) {
		return
	}

	path := c.Param("path")
	if path == "" {
		c.String(404, "404")
		return
	}
	//解码
	path, err := url.QueryUnescape(path)
	if err != nil {
		c.String(404, "404")
		return
	}
	if path == "/app.md" {
		c.File("conf/app.md")
		return
	}
	path = filepath.Join("md", path)
	os, err := os.Stat(path)
	if err != nil {
		c.String(404, "404")
		return
	}
	if os.IsDir() {
		path = filepath.Join(path, "index.md")
	}
	c.File(path)
}
